/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.domain.analysis.results;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 
 * @author jmj
 */
public abstract class Parameter implements Serializable, Cloneable {

    /**
     *
     */
    private static final long serialVersionUID = -7721577495631205185L;
    private ParameterCoord initPos;
    private ParameterCoord endPos;
    private String description;

    /**
     * @return the initPos
     */
    public ParameterCoord getInitPos() {
        return this.initPos;
    }

    /**
     * @param initPos
     *            the initPos to set
     */
    public void setInitPos(ParameterCoord initPos) {
        this.initPos = initPos;
    }

    /**
     * @return the endPos
     */
    public ParameterCoord getEndPos() {
        return this.endPos;
    }

    /**
     * @param endPos
     *            the endPos to set
     */
    public void setEndPos(ParameterCoord endPos) {
        this.endPos = endPos;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * @param description
     *            the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return value as formatted string
     */
    public String getValueAsString() {
        DecimalFormat format = new DecimalFormat("###0.00");
        return format.format(this.calculateParamValue());
    }

    /**
     * Calcula en base a sus parametros el valor contenido
     *
     * @return parameter value
     */
    public abstract BigDecimal calculateParamValue();

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((this.description == null) ? 0 : this.description.hashCode());
        result = prime * result + ((this.endPos == null) ? 0 : this.endPos.hashCode());
        result = prime * result + ((this.initPos == null) ? 0 : this.initPos.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Parameter other = (Parameter) obj;
        if (this.description == null) {
            if (other.description != null) {
                return false;
            }
        } else if (!this.description.equals(other.description)) {
            return false;
        }
        if (this.endPos == null) {
            if (other.endPos != null) {
                return false;
            }
        } else if (!this.endPos.equals(other.endPos)) {
            return false;
        }
        if (this.initPos == null) {
            if (other.initPos != null) {
                return false;
            }
        } else if (!this.initPos.equals(other.initPos)) {
            return false;
        }
        return true;
    }

    @Override
    public Object clone() {
        Parameter copyParam = null;
        try {
            copyParam = (Parameter) super.clone();
            copyParam.setDescription(new String(this.getDescription()));
            copyParam.setInitPos((ParameterCoord) this.getInitPos().clone());
            copyParam.setEndPos((ParameterCoord) this.getEndPos().clone());
        } catch (CloneNotSupportedException ex) {
            ex.printStackTrace();
        }
        return copyParam;
    }
}
